package TCP.Test;

import java.io.InputStream;
import java.io.OutputStream;
import java.net.ServerSocket;
import java.net.Socket;

public class Server {
    public static void main(String[] args) throws Exception {
        //创建一个socket服务端
        ServerSocket serverSocket = new ServerSocket(8888);
        System.out.println("服务端已启动!");
        //接受消息,如果客户端发送了消息，accept会立马收到一个socket，里面装的是数据
        while (true) {
            OutputStream os = null;
            Socket socket = serverSocket.accept();
            //读取socket中的数据
            InputStream is = socket.getInputStream();
            byte[] a = new byte[1024];
            int len;
            while ((len = is.read(a)) != -1) {
                System.out.println("客户端" + socket.getInetAddress() + "说：");
                String s = new String(a, 0, len);
                System.out.println(s);

                if (s.equals("admin123")) {
                    //创建客户端返回信息
                    os = socket.getOutputStream();
                    os.write("欢迎光临".getBytes());
                } else {
                    //创建客户端返回信息
                    os = socket.getOutputStream();
                    os.write("登录失败".getBytes());

                }
            }
        }
    }
}
